package com.china08.yxyapi.api.manage.edu.platform.resource;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.service.yxyresource.YxyResourceService;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.EsResourcePvUpdateUtil;
import com.china08.yxyapi.util.EsResourceUpdateUtil;
import com.china08.yxyapi.util.YxyResUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController(value = "EduResourceDetailController")
public class ResourceDetailController extends YxyApiBaseController {

    @Autowired
    private YxyResUtil YxyResUtil;

    @Autowired
    private YxyResourceService yrs;

    @Autowired
    private YxyResResourceRepository yrrr;

    @ApiOperation(value = "教育局审核资源详情")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/edu/platform/resource/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws IllegalAccessException, InvocationTargetException {
        RespModel model = new RespModel();
        ElasticYxyResResource elasticYxyResResource = yrs.findById(reqModel.id);
        Assert.notNull(elasticYxyResResource, "找不到资源，请刷新页面");
        YxyResResource resource = yrrr.findById(reqModel.id);
        Assert.notNull(resource, "找不到资源，请刷新页面");
        resource.setPv(resource.getPv() + 1);
        yrrr.save(resource);
        BeanUtils.copyProperties(model, elasticYxyResResource);
        if (elasticYxyResResource.getUploadTime() != null) {
            model.uploadTimeFormat = DateToolUtils.format(elasticYxyResResource.getUploadTime(),
                    DateToolUtils.FORMAT_SHORT_YMD);
        }
        model.setMessage("成功");
        // 资源下载
        String download = YxyResUtil.getDownloadUrl(reqModel.id);
        model.setDownloadLocation(download);
        model.setPreview(YxyResUtil.getPreviewUrl(reqModel.id));
        model.setExtension(resource.getExtension());
        // 通知ES
        EsResourceUpdateUtil.updateEsResoure(resource.getId());
        EsResourcePvUpdateUtil.updateEsResoure(resource.getId() + "_1");
        // 所属
        model.setBookContentNames(elasticYxyResResource.getBookContentNameList());
        return model;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String id;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "请指定资源");
        }

    }

    public static class RespModel extends YxyApiRespModel {
        // id
        public String id;
        // 标题
        public String title;

        // 类型
        public String typeName;

        // 上传人名
        public String uploaderName;

        // 上传者学校明天
        public String uploaderSchoolName;

        // 审核人
        public String auditUserName;

        // 审核学校
        public String auditSchoolName;

        // 资源简介
        public String intro;
        public String extension;
        // 文件的状态信息 0初始上传， 1 转码中 ，2 转码成功 ，3 转码失败
        public Integer convertStatus = 0;

        /**
         * 文件路径
         */
        public String location;

        public String formatId;

        public String formatName;

        // 上传时间
        public String uploadTimeFormat = "";

        // 学段 + 学科 + 版本 + 侧别（课本） + 目录
        public List<String> bookContentNames;

        // 下载
        public String downloadLocation = "";

        // 预览
        public String preview = "";

        // 审核状态 0：待审核 1：已通过 2：未通过 3：已下架
        public Integer status = 0;

        public String getExtension() {
            return extension;
        }

        public void setExtension(String extension) {
            this.extension = extension;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getTypeName() {
            return typeName;
        }

        public void setTypeName(String typeName) {
            this.typeName = typeName;
        }

        public String getUploaderName() {
            return uploaderName;
        }

        public void setUploaderName(String uploaderName) {
            this.uploaderName = uploaderName;
        }

        public String getUploaderSchoolName() {
            return uploaderSchoolName;
        }

        public void setUploaderSchoolName(String uploaderSchoolName) {
            this.uploaderSchoolName = uploaderSchoolName;
        }

        public String getAuditUserName() {
            return auditUserName;
        }

        public void setAuditUserName(String auditUserName) {
            this.auditUserName = auditUserName;
        }

        public String getAuditSchoolName() {
            return auditSchoolName;
        }

        public void setAuditSchoolName(String auditSchoolName) {
            this.auditSchoolName = auditSchoolName;
        }

        public String getIntro() {
            return intro;
        }

        public void setIntro(String intro) {
            this.intro = intro;
        }

        public Integer getConvertStatus() {
            return convertStatus;
        }

        public void setConvertStatus(Integer convertStatus) {
            this.convertStatus = convertStatus;
        }

        public String getLocation() {
            return location;
        }

        public void setLocation(String location) {
            this.location = location;
        }

        public String getFormatId() {
            return formatId;
        }

        public void setFormatId(String formatId) {
            this.formatId = formatId;
        }

        public String getFormatName() {
            return formatName;
        }

        public void setFormatName(String formatName) {
            this.formatName = formatName;
        }

        public String getUploadTimeFormat() {
            return uploadTimeFormat;
        }

        public void setUploadTimeFormat(String uploadTimeFormat) {
            this.uploadTimeFormat = uploadTimeFormat;
        }

        public List<String> getBookContentNames() {
            return bookContentNames;
        }

        public void setBookContentNames(List<String> bookContentNames) {
            this.bookContentNames = bookContentNames;
        }

        public String getDownloadLocation() {
            return downloadLocation;
        }

        public void setDownloadLocation(String downloadLocation) {
            this.downloadLocation = downloadLocation;
        }

        public String getPreview() {
            return preview;
        }

        public void setPreview(String preview) {
            this.preview = preview;
        }

        public Integer getStatus() {
            return status;
        }

        public void setStatus(Integer status) {
            this.status = status;
        }

    }
}
